﻿<UserControl x:Class="BabyCloudDrives.DesktopApp.Controls.SkyDrive"
             xmlns="http://schemas.microsoft.com/netfx/2007/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:ViewModels="clr-namespace:BabyCloudDrives.DesktopApp.ViewModels"
             xmlns:controls="clr-namespace:BabyCloudDrives.DesktopApp.Controls"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
             xmlns:ViewModelBase="clr-namespace:BabyCloudDrives.DesktopApp.ViewModels.Base"
             xmlns:Converter="clr-namespace:BabyCloudDrives.DesktopApp.Converters"
             xmlns:s="clr-namespace:BabyCloudDrives.DesktopApp.Utilities" mc:Ignorable="d" d:DesignHeight="500" d:DesignWidth="650"
             x:Name="SkyDriveUC">
    <UserControl.Resources>
        <Converter:ImageTypeConverter x:Key="imageTypeConverter" />
        <Converter:FileImageTypeConverter x:Key="fileImageTypeConvert" />
        <Converter:ByteConverter x:Key="byteConverter" />
    </UserControl.Resources>
    <UserControl.DataContext>
        <ViewModels:SkyDriveViewModel />
    </UserControl.DataContext>
    <Grid>
        <DockPanel>
            <Border BorderBrush="#118FB0" BorderThickness="1" CornerRadius="5" DockPanel.Dock="Top">
                <StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
                    <Button Width="30" Height="30" Margin="10,3" Command="{Binding BackCommand}">
                        <Button.Content>
                            <Image Width="24" Height="24">
                                <Image.Source>
                                    <BitmapImage
                                        UriSource="pack://application:,,,/BabyCloudDrives.DesktopApp;component/Icons/Command/Left.png" />
                                </Image.Source>
                            </Image>
                        </Button.Content>
                        <Button.Style>
                            <Style TargetType="Button">
                                <Setter Property="IsEnabled" Value="True" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding SelectedFolder}" Value="{x:Null}">
                                        <Setter Property="IsEnabled" Value="False" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Button.Style>
                    </Button>
                </StackPanel>
            </Border>
            <Grid DockPanel.Dock="Bottom">
                <ListBox ItemsSource="{Binding Path=SkyDriveFolders}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid Height="45">
                                <StackPanel VerticalAlignment="Center" HorizontalAlignment="Stretch"
                                            Orientation="Horizontal"
                                            Width="{Binding ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListBox}}">
                                    <CheckBox Width="20" HorizontalContentAlignment="Stretch"
                                              VerticalContentAlignment="Stretch" HorizontalAlignment="Center" VerticalAlignment="Center" />
                                    <Image Width="24" Height="24" Margin="5,0"
                                           Source="{Binding Path=Type,Converter={StaticResource ResourceKey=imageTypeConverter}}" />
                                    <TextBlock Margin="5,0" Text="{Binding Name}" VerticalAlignment="Center" />
                                    <TextBlock Text="{Binding Type}" Margin="5,0" VerticalAlignment="Center" />
                                    <TextBlock Text="{Binding Count,StringFormat={}{0} files}" Margin="5,0"
                                               VerticalAlignment="Center" />
                                    <TextBlock Text="{Binding SharedWith.Access}" Margin="5,0"
                                               VerticalAlignment="Center" />
                                    <i:Interaction.Triggers>
                                        <ViewModelBase:MouseDoubleClickEventTrigger EventName="MouseDown">
                                            <ViewModelBase:InvokeDelegateCommandAction
                                                Command="{Binding DataContext.OnOpenFolder,ElementName=SkyDriveUC}"
                                                CommandParameter="{Binding DataContext,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListBoxItem}}" />
                                        </ViewModelBase:MouseDoubleClickEventTrigger>
                                    </i:Interaction.Triggers>
                                </StackPanel>
                                <StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Width="230"
                                            Margin="0,0,20,0">
                                    <Button Content="Delete" Width="60" Height="30" Margin="5,0" />
                                    <Button Content="Download" Width="80" Height="30" Margin="5,0" />
                                    <Button Content="Rename" Width="60" Height="30" Margin="5,0" />
                                    <StackPanel.Style>
                                        <Style TargetType="StackPanel">
                                            <Style.Triggers>
                                                <DataTrigger
                                                    Binding="{Binding IsSelected,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListBoxItem}}" Value="False">
                                                    <Setter Property="Visibility" Value="Hidden" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </StackPanel.Style>
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                    <ListBox.Style>
                        <Style TargetType="ListBox">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsShowFolderList}" Value="False">
                                    <Setter Property="Visibility" Value="Hidden" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ListBox.Style>
                </ListBox>
                <Border BorderBrush="#118FB0" BorderThickness="1" CornerRadius="5" Width="250" Height="45"
                        VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,10,15">
                    <Canvas DataContext="{Binding Quota}" HorizontalAlignment="Center" VerticalAlignment="Center"
                            Margin="0,0,180,20">
                        <TextBlock>
                            <TextBlock.Text>
                                <MultiBinding StringFormat="{} {0:0.00%}used {1:0,00}MB/{2:0,00}MB">
                                    <Binding Path="UsedPercentage" />
                                    <Binding Path="AvailableVal" />
                                    <Binding Path="QuotaVal" />
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                    </Canvas>
                    <Border.Style>
                        <Style TargetType="Border">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsShowFolderList}" Value="True">
                                    <Setter Property="Visibility" Value="Visible" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding IsShowFolderList}" Value="False">
                                    <Setter Property="Visibility" Value="Hidden" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                </Border>
                <ListBox ItemsSource="{Binding Path=SelectedSkyDriveFiles}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid Height="45">
                                <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"
                                            Orientation="Horizontal"
                                            Width="{Binding ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListBox}}">
                                    <CheckBox Width="20" HorizontalContentAlignment="Stretch"
                                              VerticalContentAlignment="Stretch" HorizontalAlignment="Center" VerticalAlignment="Center" />
                                    <Image Width="24" Height="24" Margin="5,0">
                                        <Image.Source>
                                            <MultiBinding Converter="{StaticResource ResourceKey=fileImageTypeConvert}">
                                                <Binding Path="Name" />
                                                <Binding Path="Type" />
                                            </MultiBinding>
                                        </Image.Source>
                                    </Image>
                                    <TextBlock Margin="5,0" Text="{Binding Name}" VerticalAlignment="Center" />
                                    <TextBlock Text="{Binding Type}" Margin="5,0" VerticalAlignment="Center" />
                                    <TextBlock
                                        Text="{Binding Size,StringFormat={}{0:0.00} MB,Converter={StaticResource byteConverter},ConverterParameter=1000000}"
                                        Margin="5,0" VerticalAlignment="Center">
                                        <TextBlock.Style>
                                            <Style TargetType="TextBlock">
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding Type}" Value="folder">
                                                        <Setter Property="Visibility" Value="Collapsed" />
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </TextBlock.Style>
                                    </TextBlock>
                                    <TextBlock Text="{Binding SharedWith.Access}" Margin="5,0"
                                               VerticalAlignment="Center" />
                                    <i:Interaction.Triggers>
                                        <ViewModelBase:MouseDoubleClickEventTrigger EventName="MouseDown">
                                            <ViewModelBase:InvokeDelegateCommandAction
                                                Command="{Binding DataContext.OnOpenFolder,ElementName=SkyDriveUC}"
                                                CommandParameter="{Binding DataContext,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListBoxItem}}" />
                                        </ViewModelBase:MouseDoubleClickEventTrigger>
                                    </i:Interaction.Triggers>
                                </StackPanel>
                                <StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Margin="0,0,10,0"
                                            Width="230">
                                    <Button Content="Delete" Width="60" Height="30" Margin="5,0" />
                                    <Button Content="Download" Width="80" Height="30" Margin="5,0"
                                            Click="DownloadClick"
                                            Command="{Binding DataContext.DownloadCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}"
                                            CommandParameter="{Binding DataContext,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListBoxItem}}" />
                                    <Button Content="Rename" Width="60" Height="30" Margin="5,0" />
                                    <StackPanel.Style>
                                        <Style TargetType="StackPanel">
                                            <Style.Triggers>
                                                <DataTrigger
                                                    Binding="{Binding IsSelected,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListBoxItem}}" Value="False">
                                                    <Setter Property="Visibility" Value="Hidden" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </StackPanel.Style>
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                    <ListBox.Style>
                        <Style>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsShowFileList}" Value="False">
                                    <Setter Property="ListBox.Visibility" Value="Hidden" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ListBox.Style>
                </ListBox>
            </Grid>
        </DockPanel>
        <controls:OAuth x:Name="ucOAuth">
            <controls:OAuth.Style>
                <Style TargetType="controls:OAuth">
                    <Setter Property="Visibility" Value="Hidden" />
                    <Style.Triggers>
                        <DataTrigger
                            Binding="{Binding Path=AccessToken,Source={x:Static s:PresetValues.ResponseValue}}" Value="{x:Null}">
                            <Setter Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </controls:OAuth.Style>
            <i:Interaction.Triggers>
                <ei:DataTrigger Binding="{Binding Path=DataContext.IsLoaded,ElementName=ucOAuth}" Value="True">
                    <i:InvokeCommandAction Command="{Binding Path=DataContext.OnLoadFolder,ElementName=SkyDriveUC}" />
                </ei:DataTrigger>
            </i:Interaction.Triggers>
        </controls:OAuth>
        <Grid Background="#4C666666">
            <StackPanel Orientation="Vertical" Height="100">
                <ProgressBar Maximum="{Binding ProgressMaxVal}" Value="{Binding ProgressCurrentVal}" Height="30"
                             Width="{Binding ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=StackPanel}}">
                    <ProgressBar.Style>
                        <Style TargetType="ProgressBar">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsShowDownloadProgressBar}" Value="False">
                                    <Setter Property="Visibility" Value="Hidden" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ProgressBar.Style>
                </ProgressBar>
                <Label Content="{Binding Path=LoadingMessage}" FontSize="24" HorizontalContentAlignment="Center"
                       VerticalContentAlignment="Center" />
            </StackPanel>
            <Grid.Style>
                <Style TargetType="Grid">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsLoading}" Value="False">
                            <Setter Property="Visibility" Value="Hidden" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Grid.Style>
        </Grid>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <i:InvokeCommandAction Command="{Binding LoadCompleted}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Grid>
</UserControl>